package m1.server.config.component;

import m1.server.config.component.componentInterface.ConnectionManagerInterfaceProvided;
import m1.server.config.component.componentInterface.ConnectionManagerInterfaceRequired;
import m1.server.config.component.port.DBQuery;
import m1.server.config.component.port.DBQueryReceive;
import m1.server.config.component.port.ExternalSocket;
import m1.server.config.component.port.ExternalSocketOut;
import m1.server.config.component.port.SecurityCheck;
import m1.server.config.component.port.SecurityCheckReceive;
import m2.component.Component;
import m2.links.InternalBinding;

public class ConnectionManager extends Component{
	
	private static ConnectionManager connectionManager = null;
	
	/**
	 * 
	 */
	private ConnectionManager(){
		ConnectionManagerInterfaceProvided connectionManagerInterfaceProvided = new ConnectionManagerInterfaceProvided();
		ConnectionManagerInterfaceRequired connectionManagerInterfaceRequired = new ConnectionManagerInterfaceRequired();
		addComponentInterfaceProvided(connectionManagerInterfaceProvided);
		addComponentInterfaceRequired(connectionManagerInterfaceRequired);
		addProvidedPort(new ExternalSocket("ExternalSocket"), new SecurityCheck("SecurityCheck"), new DBQuery("DBQuery"));
		addRequiredPort(new ExternalSocketOut("ExternalSocketOut"), new SecurityCheckReceive("SecurityCheckReceive"), new DBQueryReceive("DBQueryReceive"));
		connectionManagerInterfaceProvided.buildService();
		connectionManagerInterfaceRequired.buildService();
		
		InternalBinding internalBindingIn = new InternalBinding();
		internalBindingIn.internalBindingIn( getProvidedPort("ExternalSocket"), null, null, getProvidedPort("SecurityCheck"));
		
		InternalBinding internalBindingOutReturnConnection = new InternalBinding();
		internalBindingOutReturnConnection.internalBindingOut( getRequiredPort("DBQueryReceive"), null, null, getRequiredPort("ExternalSocketOut"));
				
	}
	
	/**
	 * 
	 * @return
	 */
	public static ConnectionManager getInstance(){
		if (connectionManager == null){
			connectionManager = new ConnectionManager();
		}
		return connectionManager;
	}

}
